// pages/movies/movies.js
var util = require('../../utils/util')
var app = getApp()
Page({


  /**
   * 页面的初始数据
   */
  data: {
    inTheaters: {},
    comingSoon: {},
    top250: {},
    containerShow: true,
    searchPanelShow: false,
    searchResult: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let doubanUrl = app.globalData.doubanBase
    let inTheatersUrl = doubanUrl + "/v2/movie/in_theaters" + "?start=0&count=3"
    let comingSoonUrl = doubanUrl + "/v2/movie/coming_soon" + "?start=0&count=3"
    let top250Url = doubanUrl + "/v2/movie/top250" + "?start=0&count=3"
    this.getMovieListData(inTheatersUrl, "inTheaters", "正在热映")
    this.getMovieListData(comingSoonUrl, "comingSoon", "即将上映")
    this.getMovieListData(top250Url, "top250", "豆瓣Top250")
  },
  onMoreTap(event) {
    let category = event.currentTarget.dataset.category
    wx.navigateTo({
      url: 'more-movie/more-movie?category=' + category
    })
  },
  getMovieListData(url, settedKey, categoryTitle) {
    let that = this
    wx.request({
      url,
      data: {},
      method: 'GET',
      header: {
        "content-Type": "application/json"
      },
      success(res) {
        that.processDoubanData(res.data, settedKey, categoryTitle)
      },
      fail(error) {
        console.log('调用失败')
        console.log(error)
      }
      // complete () {

      // }
    })
  },
  processDoubanData(moviesDouban, settedKey, categoryTitle) {
    let movies = []
    for (let idx in moviesDouban.subjects) {
      let subject = moviesDouban.subjects[idx]
      let title = subject.title
      if (title.length >= 6) {
        title = title.substring(0, 6) + "..."
      }
      let temp = {
        stars: util.convertToStarsArray(subject.rating.stars),
        title,
        average: subject.rating.average,
        coverageUrl: subject.images.large,
        movieId: subject.id
      }
      movies.push(temp)
    }
    let readyData = {}
    readyData[settedKey] = {
      categoryTitle,
      movies
    }
    this.setData(readyData)
  },
  onBindFocus(event) {
    this.setData({
      containerShow: false,
      searchPanelShow: true
    })
  },
  onBindConfirm(event) {
    let text = event.detail.value
    let searchUrl = app.globalData.doubanBase + "/v2/movie/search?q=" + text
    this.getMovieListData(searchUrl, "searchResult", "")
  },
  onCancelImgTap(event) {
    this.setData({
      containerShow: true,
      searchPanelShow: false,
      searchResult: {}
    })
  },

  onMovieTap (event) {
    let movieId = event.currentTarget.dataset.movieId
    wx.navigateTo({
      url: 'movie-detail/movie-detail?movieId=' + movieId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})